package com.example.xpvideo.service.serviceImpl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.PhpMerchantsApplyReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.req.UserListReq;
import com.example.xpvideo.entity.res.PhpLocalAdminRes;
import com.example.xpvideo.entity.res.PHPMerchantsApplyListRes;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.*;
import com.example.xpvideo.service.PhpMerchantsApplyService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/8/26 14:20
 * @description:
 */
@Service
public class PhpMerchantsApplyServiceImpl implements PhpMerchantsApplyService {
    @Autowired
    private PhpMerchnatsApplyMapper phpMerchnatsApplyMapper;
    @Autowired
    private LsShopApplyMapper lsShopApplyMapper;
    @Autowired
    private LsShopMapper lsShopMapper;
    @Autowired
    private QualificationMapper qualificationMapper;
    @Autowired
    private LsShopAdminMapper lsShopAdminMapper;
    @Autowired
    private LsUserMapper lsUserMapper;
    @Autowired
    private LiveBroadcastMapper liveBroadcastMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<PHPMerchantsApplyListRes>>> list(PhpMerchantsApplyReq phpMerchantsApplyReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String nickname = phpMerchantsApplyReq.getNickname();
        String mobile = phpMerchantsApplyReq.getMobile();
        Integer type = phpMerchantsApplyReq.getType();
        String storeName = phpMerchantsApplyReq.getStoreName();
        String name = phpMerchantsApplyReq.getName();
        String account = phpMerchantsApplyReq.getAccount();


        PageHelper.startPage(page, size);
        List<PHPMerchantsApplyListRes> list = phpMerchnatsApplyMapper.MerchantsApplyList(Wrappers.<LsShopApply>query()
                .eq(!ObjectUtils.isEmpty(account), "account", account)
                        .eq(!ObjectUtils.isEmpty(nickname),"nickname",nickname)
                        .eq(!ObjectUtils.isEmpty(mobile),"mobile",mobile)
                        .eq(!ObjectUtils.isEmpty(type),"type",type)
                        .eq(!ObjectUtils.isEmpty(storeName),"store_name",storeName)
                        .eq(!ObjectUtils.isEmpty(name),"name",name)

                .eq("audit_status", 1));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PHPMerchantsApplyListRes>> listPageResult = new PageResult<>(count, pages,
                pageSize, pageNum,list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> merchantsAudit(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();


        LsShopApply lsShopApply = lsShopApplyMapper.selectOne(Wrappers.<LsShopApply>query().eq("id",id));
        LsUser lsUser = lsUserMapper.selectOne(Wrappers.<LsUser>query().eq("id",lsShopApply.getUserId()));
        LsShopAdmin lsShopAdmin = new LsShopAdmin();

        lsShopAdmin.setRoot(0);
        lsShopAdmin.setName(lsShopApply.getName());
        lsShopAdmin.setAccount(lsUser.getAccount());
        lsShopAdmin.setPassword(lsUser.getPassword());
        lsShopAdmin.setSalt(lsUser.getSalt());
        lsShopAdmin.setCreateTime(String.valueOf(timeService.getTime()));
        lsShopAdmin.setDisable(0);
        lsShopAdmin.setDel(0);
        lsShopAdmin.setPhone(lsUser.getMobile());
        lsShopAdmin.setInvitationCode(lsShopApply.getCode());
        lsShopAdmin.setSuperAccount(lsShopApply.getSuperAccount());
        lsShopAdmin.setUserId(lsUser.getId());

        int res1 = lsShopAdminMapper.insertLsShopAdmin(lsShopAdmin);

        if(res1 > 0){
            LsShopAdmin lsShopAdmin1 = lsShopAdminMapper.selectOne(Wrappers.<LsShopAdmin>query().eq("account",lsUser.getAccount()));

            LsShop lsShop = new LsShop();

            lsShop.setCid(lsShopApply.getCid());
            lsShop.setType(1);
            lsShop.setName(lsShopApply.getStoreName());
            lsShop.setMobile(lsUser.getMobile());
            lsShop.setNickname(lsShopApply.getNickname());
            lsShop.setIntro(lsShopApply.getIntro());
            lsShop.setProvinceId(lsShopApply.getProvinceId());
            lsShop.setCityId(lsShopApply.getCityId());
            lsShop.setDistrictId(lsShopApply.getDistrictId());
            lsShop.setIsRun(1);
            lsShop.setIsFreeze(0);
            lsShop.setDel(0);
            lsShop.setOperationType(lsShopApply.getOperationType());

            lsShop.setCreateTime(timeService.getTime());
            lsShop.setMerchantsId(lsShopAdmin1.getId());
            lsShop.setDistrictId(lsShopApply.getDistrictId());
            lsShop.setWallet(BigDecimal.valueOf(0.0));
            lsShop.setTradeServiceFee(BigDecimal.valueOf(0.00));
            LiveBroadcast liveBroadcast = new LiveBroadcast();
            liveBroadcast.setMerchantsId(lsShopAdmin1.getId());

            int res = liveBroadcastMapper.insertLiveBroadcast(liveBroadcast);

            LiveBroadcast liveBroadcast1 = liveBroadcastMapper.selectOne(Wrappers.<LiveBroadcast>query().eq("merchants_id",lsShopAdmin1.getId()));

            lsShop.setLiveId(liveBroadcast1.getId());

            int res2 = lsShopMapper.insert(lsShop);

            Qualification qualification = new Qualification();

            qualification.setMerchantsId(lsShopAdmin1.getId());
            qualification.setStatus(0);

            int res3 = qualificationMapper.update(qualification,Wrappers.<Qualification>update().eq("merchants_id",lsShopApply.getId()
            ).eq("status",1));

            if((res > 0 && res2 > 0) ){
                LsShopApply lsShopApply1 = new LsShopApply();
                lsShopApply1.setId(id);
                lsShopApply1.setAuditStatus(2);
                lsShop.setUpdateTime(timeService.getTime());

                int res4 = lsShopApplyMapper.update(lsShopApply1,Wrappers.<LsShopApply>update().eq("id",id));


                return ResponseResult.responseResult(0,"审核成功");
            }

        }
        if(stage == 3){
            LsShopApply lsShopApply1 = new LsShopApply();
            lsShopApply1.setId(id);
            lsShopApply1.setAuditStatus(3);
            lsShopApply1.setUpdateTime(timeService.getTime());

            Qualification qualification1 = new Qualification();
            qualification1.setMerchantsId(id);
            qualification1.setStatus(2);

            qualificationMapper.updateQualification(qualification1);
            lsShopApplyMapper.update(lsShopApply1,Wrappers.<LsShopApply>update().eq("id",id));
        }
        return null;
    }

    @Override
    public ResponseResult<Object> LocalmerchantsAudit(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();


        LsShopApply lsShopApply = lsShopApplyMapper.selectOne(Wrappers.<LsShopApply>query().eq("id",id));
        LsUser lsUser = lsUserMapper.selectOne(Wrappers.<LsUser>query().eq("id",lsShopApply.getUserId()));
        LsShopAdmin lsShopAdmin = new LsShopAdmin();
        lsShopAdmin.setRoot(lsShopApply.getType());
        lsShopAdmin.setName(lsShopApply.getName());
        lsShopAdmin.setAccount(lsUser.getAccount());
        lsShopAdmin.setPassword(lsUser.getPassword());
        lsShopAdmin.setSalt(lsUser.getSalt());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'");
        lsShopAdmin.setCreateTime(simpleDateFormat.format(new DateTime()));
        lsShopAdmin.setDisable(0);
        lsShopAdmin.setDel(0);
        lsShopAdmin.setPhone(lsUser.getMobile());
        lsShopAdmin.setInvitationCode(lsShopApply.getCode());
        lsShopAdmin.setSuperAccount(lsShopApply.getSuperAccount());
        lsShopAdmin.setUserId(lsUser.getId());

        int res1 = lsShopAdminMapper.insertLsShopAdmin(lsShopAdmin);

        if(res1 > 0){
            LsShopAdmin lsShopAdmin1 = lsShopAdminMapper.selectOne(Wrappers.<LsShopAdmin>query().eq("account",lsUser.getAccount()));

            LsShop lsShop = new LsShop();

            lsShop.setCid(lsShopApply.getCid());
            lsShop.setType(2);
            lsShop.setName(lsShopApply.getStoreName());
            lsShop.setMobile(lsUser.getMobile());
            lsShop.setNickname(lsShopApply.getNickname());
            lsShop.setIntro(lsShopApply.getIntro());
            lsShop.setProvinceId(lsShopApply.getProvinceId());
            lsShop.setCityId(lsShopApply.getCityId());
            lsShop.setDistrictId(lsShopApply.getDistrictId());
            lsShop.setIsRun(1);
            lsShop.setIsFreeze(0);
            lsShop.setDel(0);
            lsShop.setOperationType(lsShopApply.getOperationType());
            lsShop.setCreateTime(timeService.getTime());
            lsShop.setMerchantsId(lsShopAdmin1.getId());
            lsShop.setLevel(lsShopApply.getIsSuperShop());

            LsShop admin= lsShopMapper.selectOne(Wrappers.<LsShop>query().select("merchants_id").eq("name",lsShopApply.getShopName())
                    .eq("level",0));
            lsShop.setAdminId(String.valueOf(admin.getMerchantsId()));

            int res2 = lsShopMapper.insert(lsShop);

            Qualification qualification = new Qualification();

            qualification.setMerchantsId(lsShopAdmin1.getId());
            qualification.setStatus(0);

            int res3 = qualificationMapper.update(qualification,Wrappers.<Qualification>update().eq("merchants_id",lsShopApply.getId()
            ).eq("status",1));

            if(  res2 > 0 ){
                LsShopApply lsShopApply1 = new LsShopApply();
                lsShopApply1.setId(id);
                lsShopApply1.setAuditStatus(2);
                lsShopApply1.setUpdateTime(Integer.valueOf(simpleDateFormat.format(new DateTime())));

                int res4 = lsShopApplyMapper.update(lsShopApply1,Wrappers.<LsShopApply>update().eq("id",id));

                return ResponseResult.responseResult(0,"审核成功");
            }

        }
        if(stage == 3){
            LsShopApply lsShopApply1 = new LsShopApply();
            lsShopApply1.setId(id);
            lsShopApply1.setAuditStatus(3);
            lsShopApply1.setUpdateTime(Integer.valueOf(simpleDateFormat.format(new DateTime())));

            Qualification qualification1 = new Qualification();
            qualification1.setMerchantsId(id);
            qualification1.setStatus(2);

            qualificationMapper.updateQualification(qualification1);

            lsShopApplyMapper.update(lsShopApply1,Wrappers.<LsShopApply>update().eq("id",id));
        }
        return null;
    }
}
